﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyGiaoDuc_Winform.ServiceXuLyThongTinPhongHoc;
using QuanLyGiaoDuc_Winform.ServiceXuLyThongTinLop;

namespace QuanLyGiaoDuc_Winform
{
    public partial class FormSapXepPhongHoc : Form
    {
        public FormSapXepPhongHoc()
        {
            InitializeComponent();
        }

        ServiceXuLyThongTinPhongHoc.ServiceXuLyThongTinPhongHocSoapClient proxy_phong = new ServiceXuLyThongTinPhongHocSoapClient ();
        ServiceXuLyThongTinLop.ServiceXuLyThongTinLopSoapClient proxy_lop = new ServiceXuLyThongTinLopSoapClient();
        private LOP_LayDanhSachLopHocChuaCoPhongResult[] arrLop;

        public LOP_LayDanhSachLopHocChuaCoPhongResult[] ArrLop
        {
            get { return arrLop; }
            set { arrLop = value; }
        }
        private PHONGHOC[] arrPhong;

        public PHONGHOC[] ArrPhong
        {
            get { return arrPhong; }
            set { arrPhong = value; }
        }

        private void FormSapXepPhongHoc_Load(object sender, EventArgs e)
        {
            arrLop = null;
            arrLop = proxy_lop.LayDanhSachLopChuaCoPhong();
            
            arrPhong = null;
            arrPhong = proxy_phong.LayDanhSachPhong();

            dataGrid_SapXepPhongHoc.Rows.Clear();

            for (int i = 0; i < arrLop.Count(); i++)
            {
                dataGrid_SapXepPhongHoc.Rows.Add(i+1, arrLop[i].MALOP, arrLop[i].TENLOP, arrLop[i].NGAYHOC, arrLop[i].TIETBATDAU, arrLop[i].TIETKETTHUC, arrLop[i].SLSVDK_CHINHTHUC);
            }

            ((DataGridViewComboBoxColumn)dataGrid_SapXepPhongHoc.Columns[6]).DataSource = arrPhong;
            ((DataGridViewComboBoxColumn)dataGrid_SapXepPhongHoc.Columns[6]).DisplayMember = "TENPHONG";
            ((DataGridViewComboBoxColumn)dataGrid_SapXepPhongHoc.Columns[6]).ValueMember = "MAPHONG";

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < arrLop.Count(); i++)
            {
                int maPhong = int.Parse(dataGrid_SapXepPhongHoc[6, i].Value.ToString());
                int maLop = Convert.ToInt32(dataGrid_SapXepPhongHoc[1, i].Value);

                int ketQua = proxy_lop.CapNhatPhongVaoLopHoc(maLop, maPhong);
                if (maLop == 0)
                {
                    MessageBox.Show("Không có lớp!");
                    return;
                }
                if (maPhong == 0)
                {
                    MessageBox.Show("Chưa chọn phòng ở lớp " + maLop.ToString());
                    return;
                }
                if (ketQua == 0)
                {
                    MessageBox.Show("Có lỗi tại lớp" + maLop.ToString());
                    return;
                }
            }
            MessageBox.Show("Đã cập nhật phòng học vào lớp thành công!");
        }

        private void dataGrid_SapXepPhongHoc_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}